package com.john.utils.common;

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;

public class Base64Util {
        private static final String charset = "utf-8";
//    private static final String charset = "GB2312";

    /**
     * 解密
     *
     * @param data
     * @return
     * @author fanddong
     */
    public static String decode(String data) {
        try {
            if (null == data) {
                return null;
            }

            return new String(Base64.decodeBase64(data.getBytes(charset)), charset);
        } catch (UnsupportedEncodingException e) {
            System.out.println(String.format("字符串：%s，解密异常" , data));
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 加密
     *
     * @param data
     * @return
     * @author fanddong
     */
    public static String encode(String data) {
        try {
            if (null == data) {
                return null;
            }
            return new String(Base64.encodeBase64(data.getBytes(charset)), charset);
        } catch (UnsupportedEncodingException e) {
            System.out.println(String.format("字符串：%s，加密异常", data));
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        System.out.println(encode("REPORT__Z_776"));
        System.out.println(decode("UkVQT1JUX19aXzc3Ng=="));

        //code--------F80E753B8926381680E753B89EU0001169D6087B5F80U0007363UkVQT1JUX19aXzc3Ng==
    }

}